import QtQuick 1.0
import "Article.js" as ArticleUtil
import "CommomUtil.js" as CommonUtil
import com.meego 1.0
import "const.js" as Const

Item {
    id: articleListItem



    property string articleType:"focus";

    property bool loading: focusImageModel.status == XmlListModel.Loading
    //文章内容页面
    property string page_url:"http://sp.autohome.com.cn/Nokia/ArticlePageList.ashx?type=1&Id="

    width: 480
    height: articleListItem.articleType === "focus" ?  240 : 127


    ArticleFocusImageModel{
        id:focusImageModel
    }

    ListModel{
        id: empty_image_model
    }

    Item{
        id:focus_rect
        width: parent.width; height: articleListItem.article_type === "focus" ? 240 : 0
        anchors.top: parent.top

        opacity: articleListItem.articleType === "focus" ? 1 : 0

        ListView {
            focus: true
            id: focus_image_listview
            anchors.fill: parent
            model: articleListItem.articleType === "focus" ? focusImageModel : empty_image_model
            delegate: ArticleFocusImagesList {}
            orientation: "Horizontal"

        }
    }
  Item{
        id:list_rect
        width: parent.width; height: articleListItem.articleType !== "focus" ? parent.height :0
        anchors.top: parent.top

        opacity: articleListItem.articleType !== "focus" ? 1 : 0

        BorderImage{
            id:content_back_image
            anchors.fill: parent
            source: "../images/articles/listBack.png"

          }

        Image{
              id:article_image
              width: 80
              height: 60
              source: articleImage
              anchors.left: content_back_image.left; anchors.leftMargin: 35
              anchors.top: content_back_image.top; anchors.topMargin: 30
              fillMode: Image.PreserveAspectFit
              smooth: true
              //anchors.right:parent.right
             anchors.verticalCenter:parent.verticalCenter
           //   anchors.horizontalCenter: parent.horizontalCenter

           }

        Rectangle{
            id:article_content_rect
            width: parent.width - 142; height: parent.height
            anchors.left: article_image.right; anchors.leftMargin: 15
            anchors.top: parent.top
            opacity: 0
        }

        Column {
               id: column
               width: parent.width
                anchors.fill: article_content_rect
               Text {
                    id: article_title
                    width: article_content_rect.width; height: 54
                    anchors.left: parent.left; anchors.top: parent.top
                    anchors.topMargin: 7
                    text: articleTitle
                    wrapMode: Text.WordWrap
                    font { bold: true; family: "Helvetica"; pointSize: 15 }
                    horizontalAlignment: Text.AlignLeft
                }

                Text {
                    id:article_content
                    width:article_content_rect.width; height: 45
                    anchors.left: article_content_rect.left; anchors.top: article_title.bottom
                    text: ArticleUtil.formatAricleContent(articleContent)
                    wrapMode: Text.WordWrap
                    font { bold: false; family: "Helvetica"; pointSize: 14 }
                     horizontalAlignment: Text.AlignLeft
                }

                Text {
                    id:article_time
                    width: article_content_rect.width; height: 16
                    anchors.left: article_content_rect.left; anchors.top: article_content.bottom
                    anchors.bottomMargin: 5
                    text:articlePublishTime
                    wrapMode: Text.WordWrap
                    font { bold: false; family: "Helvetica"; pointSize: 12 }
                     horizontalAlignment: Text.AlignLeft
                }

          }

        MouseArea {
            anchors.fill: list_rect
            onClicked: {
                console.log("get current articleId:"+articleId);//传递参数：articleId 到具体文章页面，根据此参数获取文章页面

                forwardToContent(articleId);

            }

        }
   }
    function forwardToContent(articleId){
        console.log("forwardToContent:"+articleId);
        ArticleUtil.setArticleId(articleId); //文章点击 查看详细，设置文章id

      console.log("content url:"+ArticleUtil.getContentUrl(articleId));

        var pageNum="";
        var doc = new XMLHttpRequest();
        doc.onreadystatechange = function() {
            if (doc.readyState == XMLHttpRequest.HEADERS_RECEIVED) {
            } else if (doc.readyState == XMLHttpRequest.DONE) {
                var a = doc.responseXML.documentElement;

                console.log("a.childNodes[1].nodeValue:"+a.childNodes[1].childNodes[1].nodeName);
                console.log("a.childNodes[1].childNodes.length:"+a.childNodes[1].childNodes[1].childNodes[0].nodeValue);


                pageNum =a.childNodes[1].childNodes[1].childNodes[0].nodeValue;

                console.log(pageNum);

                ArticleUtil.setPageNum(pageNum);//

                mainPage.pageStack.push(Qt.resolvedUrl("ArticleContent.qml"));
            }
        }

       doc.open("GET", ArticleUtil.getContentUrl(articleId));
        doc.send();
        console.log("aricle page num:"+pageNum);

    }

}
